package com.example.actearly_revised;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;

@TargetApi(11)
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

	//final Calendar;
	int year;
	int month;
	int day;
	EditText text;
	Boolean showing;
	public Boolean getShowing() {
		return showing;
	}

	@SuppressLint("NewApi")
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Use the current date as the default date in the picker
		final Calendar c = Calendar.getInstance();

		//if(getDate().substring(0).equals("0") && getDate().substring(1).equals("0") ){
		Context getContext = getActivity();
		SQLogic conn = new SQLogic(getContext);
		conn.open();

		User currentUser = conn.getUserData(LoginView.currentProfile);
		conn.close();
		if(currentUser ==  null){
			System.out.println("LLLLLLLLLLLL if");
			this.year = c.get(Calendar.YEAR);
			this.month = c.get(Calendar.MONTH);
			this.day = c.get(Calendar.DAY_OF_MONTH);
		}
		else{
			System.out.println("LLLLLLLLLLLL else");
			System.out.println(">>>"+currentUser.year);
			System.out.println(">>>"+currentUser.month);
			System.out.println(">>>"+currentUser.day);

			currentUser.getAge(currentUser.Birthdate);
			this.year = currentUser.year;
			this.month = currentUser.month;
			this.day = currentUser.day;

			//showing = false;
		}

		//}
		// Create a new instance of DatePickerDialog and return it
		return new DatePickerDialog(getActivity(), this, this.year, this.month, this.day);
	}

	public String getDate(){
		//newPick = new DatePicker(getActivity());
		//month = newPick.getMonth();
		//year = newPick.getYear();
		//day = newPick.getDayOfMonth();
		String monthS = Integer.toString(month);
		String dayS  = Integer.toString(day);
		String yearS = Integer.toString(year);
		if (monthS.length()==1)
		{monthS="0"+monthS;
		}

		if (dayS.length()==1)
		{
			dayS="0"+dayS;
		}
		System.out.println(monthS+dayS+yearS);
		String bdate=monthS+"/"+ dayS+"/"+yearS;
		return bdate;

	}

	public EditText getEdit(EditText input){
		text = input;
		return input;

	}

	@Override
	public void onStop(){
		super.onStop();
		System.out.println("SDFADAFDSFADS");

		text.setText(getDate());
		showing = true;
	}

	public void onDateSet(DatePicker view, int year, int month, int day) {
		System.out.println("sa");
		this.year = year;
		if(month < 12){
			this.month = month +1;
		}
		else{
			month = 1;
		}
		this.day = day;
		System.out.println("" + year +" " +month + " " + day);
		// Do something with the date chosen by the user
	}
}